//
//  ConsultingVC.m
//  YZZX
//
//  Created by Easaa on 15/5/5.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "ConsultingVC.h"
#import "NSString+XSZCommon.h"


@interface ConsultingVC ()<UIScrollViewDelegate,HXHttpRequestDelegate,UITextFieldDelegate,UITextViewDelegate>
{
    UILabel     *NameLable;
    UITextField *Nametextfild;
    
    UILabel     *PhoneLable;
    UITextField *Phonetextfild;
    
    UILabel     *EmailLable;
    UITextField *Emailtextfild;

    UILabel     *MassgeLable;
    UITextView  *MassgeTextView;

    UIScrollView    *_BgScrollView;
    
    
    HXHttpRequest   *_httpRequest;
    
}

@end

@implementation ConsultingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // UIScrollView创建
    _BgScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Dev_ScreenWidth, Dev_ScreenHeight-64)];
    _BgScrollView.contentSize =CGSizeMake(Dev_ScreenWidth, Dev_ScreenHeight+250);
    _BgScrollView.delegate =self;
    //_BgScrollView.bounces = NO;
    //[_BgScrollView setPagingEnabled:YES];
    _BgScrollView.showsHorizontalScrollIndicator = NO;
    _BgScrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_BgScrollView];

    

    NameLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, Dev_ScreenWidth-20, 30)];
    NameLable.font = [UIFont boldSystemFontOfSize:16];  //UILabel的字体大小
    NameLable.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    NameLable.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    NameLable.text =@"姓名：";
    [_BgScrollView addSubview:NameLable];
    
    Nametextfild =[[UITextField alloc]initWithFrame:CGRectMake(10, 10+30+10, Dev_ScreenWidth-20, 30)];
    Nametextfild.tag =0;
    Nametextfild.delegate =self;
    [Nametextfild.layer setMasksToBounds:YES];
    [Nametextfild.layer setCornerRadius:3]; //设置矩圆角半径
    [Nametextfild.layer setBorderWidth:1.0];   //边框宽度
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 0, 0, 0, 0.5 });
    [Nametextfild.layer setBorderColor:colorref];//边框颜色
    [_BgScrollView addSubview:Nametextfild];
    
    PhoneLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10+30+10+30+10, Dev_ScreenWidth-20, 30)];
    PhoneLable.font = [UIFont boldSystemFontOfSize:16];  //UILabel的字体大小
    PhoneLable.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    PhoneLable.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    PhoneLable.text =@"电话：";
    [_BgScrollView addSubview:PhoneLable];
    
    Phonetextfild =[[UITextField alloc]initWithFrame:CGRectMake(10, 10+30+10+30+10+30+10, Dev_ScreenWidth-20, 30)];
    Phonetextfild.tag =1;
    Phonetextfild.delegate =self;
    Phonetextfild.clearButtonMode = UITextFieldViewModeAlways;
    [Phonetextfild.layer setMasksToBounds:YES];
    [Phonetextfild.layer setCornerRadius:3]; //设置矩圆角半径
    [Phonetextfild.layer setBorderWidth:1.0];   //边框宽度
    [Phonetextfild.layer setBorderColor:colorref];//边框颜色
    [_BgScrollView addSubview:Phonetextfild];

    Phonetextfild.keyboardType =UIKeyboardTypeNumberPad;
    
    
    
    
    
    
    EmailLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10+30+10+30+10+30+10+30+10, Dev_ScreenWidth-20, 30)];
    EmailLable.font = [UIFont boldSystemFontOfSize:16];  //UILabel的字体大小
    EmailLable.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    EmailLable.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    EmailLable.text =@"邮箱";
    [_BgScrollView addSubview:EmailLable];
    
    Emailtextfild =[[UITextField alloc]initWithFrame:CGRectMake(10, 10+30+10+30+10+30+10+30+10+30+10, Dev_ScreenWidth-20, 30)];
    Emailtextfild.tag =2;
    Emailtextfild.delegate =self;
    [Emailtextfild.layer setMasksToBounds:YES];
    [Emailtextfild.layer setCornerRadius:3]; //设置矩圆角半径
    [Emailtextfild.layer setBorderWidth:1.0];   //边框宽度
    [Emailtextfild.layer setBorderColor:colorref];//边框颜色
    [_BgScrollView addSubview:Emailtextfild];
    
    
    
    
    
    
    
    MassgeLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 10+30+10+30+10+30+10+30+10+30+10+30+10, Dev_ScreenWidth-20, 30)];
    MassgeLable.font = [UIFont boldSystemFontOfSize:16];  //UILabel的字体大小
    MassgeLable.numberOfLines = 0;  //必须定义这个属性，否则UILabel不会换行
    MassgeLable.textAlignment = NSTextAlignmentLeft;  //文本对齐方式
    //[labelTitle setBackgroundColor:[UIColor redColor]];
    MassgeLable.text =@"内容";
    [_BgScrollView addSubview:MassgeLable];
    
    MassgeTextView =[[UITextView alloc]initWithFrame:CGRectMake(10, 10+30+10+30+10+30+10+30+10+30+10+30+10+30+10, Dev_ScreenWidth-20, 100)];
    MassgeTextView.tag =3;
    MassgeTextView.delegate=self;
    [MassgeTextView.layer setMasksToBounds:YES];
    [MassgeTextView.layer setCornerRadius:3]; //设置矩圆角半径
    [MassgeTextView.layer setBorderWidth:1.0];   //边框宽度
    [MassgeTextView.layer setBorderColor:colorref];//边框颜色
    [_BgScrollView addSubview:MassgeTextView];


    
    
    
    
    
    
    
    UIButton *bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
    bottomButton.tag = 2014 ;
    bottomButton.frame = CGRectMake(10, 420, Dev_ScreenWidth-20, 44);
    [bottomButton addTarget:self action:@selector(tabBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [bottomButton.layer setMasksToBounds:YES];
    [bottomButton.layer setCornerRadius:2]; //设置矩圆角半径
    [bottomButton.layer setBorderWidth:0];   //边框宽度
    [bottomButton setTitle:@"确认" forState:UIControlStateNormal];
    [bottomButton setTintColor:[UIColor whiteColor]];
    bottomButton.backgroundColor =RGBCOLOR(252, 78, 125);
    
    [_BgScrollView addSubview:bottomButton];
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
    
    if (theTextField == Emailtextfild)
    {
        [Emailtextfild resignFirstResponder];
        
    }else if (theTextField == Phonetextfild)
    {
        [Phonetextfild resignFirstResponder];
    
    }else if (theTextField == Nametextfild)
    {
        [Nametextfild resignFirstResponder];
    
    }
  
    return YES;
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder]; return NO;
    }
    return YES; 
}

-(void)tabBarButtonClicked
{
    
    
    if(Nametextfild.text.length<=0||Phonetextfild.text.length<=0||Emailtextfild.text.length<=0||MassgeTextView.text<=0)
    {
        
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"请输入完整"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1023;
        [AlertView show];
        return;

    }else if(![Phonetextfild.text isMobile])
    {
        
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"请输入正确手机号"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1024;
        [AlertView show];
        return;
        
    }else if (![Emailtextfild.text isEmail])
    {
        UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                            message:@"请输入正确邮箱"
                                                           delegate:self
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        
        AlertView.tag = 1025;
        [AlertView show];
        return;
        
    }
        
  
    NSMutableDictionary *Dicfankui =[[NSMutableDictionary alloc]init];
    [Dicfankui setObject:Nametextfild.text forKey:@"Name"];
    [Dicfankui setObject:Phonetextfild.text forKey:@"Telephone"];
    [Dicfankui setObject:Emailtextfild.text forKey:@"Email"];
    [Dicfankui setObject:MassgeTextView.text forKey:@"Content"];

    //列表请求
    NSDictionary* dicParams = [NSDictionary dictionaryWithObjectsAndKeys:
                               [XSZ_UICommon useridChangUaerdfLoginBoolString], @"user_id",
                               @"1",                @"site_id",
                               [NSNumber numberWithInteger:[self.type integerValue]],    @"info_type",
                               Dicfankui,    @"post_data",
                               nil];
    
//    
//    "user_id": "1",
//    "site_id": "1",
//    "info_type": {{ info_type }},
//    "post_data":
//    {
//        "Name": "{{ Name }}",
//        "Telephone": "{{ Telephone }}",
//        "Email": "{{ Email }}",
//        "Content": "{{ Content }}"
//    }
    
    if (_httpRequest == nil)
    {
        _httpRequest = [[HXHttpRequest alloc] init];
        _httpRequest.delegate = self;
        
        
        
    }
    [_httpRequest requestForParams:dicParams andMethod:YZ_BOOKMESSAGE_POST andTag:1023];//@"system.time"];//
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
}



#pragma mark Http request delegate
- (void)requestFinished:(NSString *)responseString andStatusCode:(int)statusCode andTag:(NSInteger)tag
{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    //tableview列表数据
    
    if(tag == 1023)
    {
        
        NSDictionary *DicJosn = [[NSDictionary alloc]init];
        DicJosn = [JsonUtil jsonToDic:responseString];
        
        if([DicJosn[@"is_success"]boolValue])
        {
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }else
        {
            
            UIAlertView  *AlertView = [[UIAlertView alloc]initWithTitle:@"提示"
                                                                message:DicJosn[@"message"]
                                                               delegate:self
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
            
            AlertView.tag = 1024;
            [AlertView show];
            return;
            
        }
        
    }
}

- (void)requestFailed:(NSError *)error andTag:(NSInteger)tag
{
    
}

//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
//{
//    if (1 == range.length) {//按下回格键
//        return YES;
//    }
//    if ([text isEqualToString:@"\n"]) {//按下return键
//        //这里隐藏键盘，不做任何处理
//        [textView resignFirstResponder];
//        return NO;
//    }
//    return NO;
//}
//
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    [self.view endEditing:YES];
//}


@end
